package com.mallsystem.orderservice.controller;

import com.mallsystem.common.bean.Cart;
import com.mallsystem.common.bean.RespBean;
import com.mallsystem.common.exception.AllException;
import com.mallsystem.common.service.CartService;
import com.mallsystem.common.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;
    //添加
    @PostMapping("/{goodsId}")
    public RespBean insert(@PathVariable("goodsId") Integer goodsId, @RequestHeader("token") String token){
        //解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");
        //创建购物车对象
        Cart cart = new Cart();
        cart.setUserId(userId);
        cart.setGoodsId(goodsId);
        //添加
        cartService.insertOrUpdate(cart);
        return RespBean.ok("成功添加到购物车");
    }
    @DeleteMapping("/{id}")
    public RespBean delete(@PathVariable("id") Integer id,@RequestHeader("token") String token) throws AllException {
        //解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");

        cartService.delete(id,userId);

        return RespBean.ok("删除成功");
    }
    @PutMapping
    public RespBean update(@RequestBody Cart cart,@RequestHeader("token") String token) throws AllException {
        //解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");

        cartService.update(cart,userId);

        return RespBean.ok("成功修改购物车");
    }
    //查询已登录用户所有购物车信息
    @GetMapping
    public RespBean selectCartList(@RequestHeader("token") String token){
        //解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");

        Cart condition = new Cart();
        condition.setUserId(userId);
        List<Cart> cartList = cartService.search(condition);
        return RespBean.ok("",cartList);
    }
    @GetMapping("/{id}")
    public RespBean selectById(@PathVariable("id") Integer id){
        Cart cart = cartService.selectById(id);
        return RespBean.ok("",cart);
    }
    @DeleteMapping("/deleteBatch")
    public RespBean deleteBatch(@RequestBody Map<String, ArrayList<Integer>> data, @RequestHeader("token") String token) throws AllException {
        //解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");
        //获取被删除的购物车id
        ArrayList<Integer> ids = data.get("ids");
        cartService.deleteBatch(ids,userId);
        return RespBean.ok("批量删除成功");
    }
    @GetMapping("/ids")
    public RespBean selectByIds(Integer[] cartIds,@RequestHeader("token") String token) throws AllException {
        System.out.println(cartIds);
        //解析token获取用户id
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer userId = (Integer) map.get("id");
        List<Cart> cartList = cartService.selectByIds(cartIds,userId);
        return RespBean.ok("查询成功",cartList);
    }
}
